#!/bin/bash

# Slackware build script for Azureus/Vuze
# Written by Chris Lawrence <selivanow@gmail.com>

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=vuze
VERSION=${VERSION:-4604}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

if [ "$ARCH" = "x86_64" ]; then
  LIBDIRSUFFIX="64"
  ARCHIVE=Vuze_${VERSION}_linux-$ARCH.tar.bz2
else
  ARCH=i486
  LIBDIRSUFFIX=""
  ARCHIVE=Vuze_${VERSION}_linux.tar.bz2
fi

rm -rf $PKG
mkdir -p $TMP $PKG/usr/lib${LIBDIRSUFFIX} $PKG/usr/bin $OUTPUT
tar xvf $CWD/$ARCHIVE -C $PKG/usr/lib${LIBDIRSUFFIX}

# Set PROGRAM_DIR and MOZILLA_FIVE_HOME in the azureus program script
sed "s%@LIBDIR@%/usr/lib${LIBDIRSUFFIX}%g" $CWD/vuze-set_paths.diff | \
    patch -p1 -d $PKG/usr/lib${LIBDIRSUFFIX}/vuze

# Create two new symlinks in /usr/bin
( cd $PKG/usr/bin
  ln -s ../lib${LIBDIRSUFFIX}/$PRGNAM/azureus
  ln -s ../lib${LIBDIRSUFFIX}/$PRGNAM/vuze
)

mkdir -p $PKG/usr/share/{applications,pixmaps}
cat $CWD/vuze.desktop > $PKG/usr/share/applications/vuze.desktop
mv $PKG/usr/lib${LIBDIRSUFFIX}/vuze/*.png $PKG/usr/share/pixmaps

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a\
  $PKG/usr/lib${LIBDIRSUFFIX}/vuze/README.txt \
  $PKG/usr/lib${LIBDIRSUFFIX}/vuze/TOS.txt \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# Clean up some cruft.
rm $PKG/usr/lib${LIBDIRSUFFIX}/vuze/vuze.desktop
rm $PKG/usr/lib${LIBDIRSUFFIX}/vuze/*.txt
rm $PKG/usr/lib${LIBDIRSUFFIX}/vuze/*.log

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
